function out=dummy_full(FE)
%%% This function creates non-collinear dummies for categorical variables%%
%%% (exclude constant variable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get number of variables and number of observations
N=size(FE,1); %number of observations

% Create Dummies for Categorical Variables,
% Create one constant variable but omit the 1st dummy in each cat. var.
  % Get unique values of each FE
  f=size(FE,2); uFE=cell(f,1); t=NaN(f,1); %each cell is a cat. var.
  for i=1:f, uFE{i}=unique(FE(:,i)); t(i)=length(uFE{i}); end; 
  
  % Preallocate, including constant 
  %First dummy is constant,
  % # of dummies = 1 + (#of unique values of cat. vars - 1), omit 1 dummy
  % in each cat var.
  Kf=0; for i=1:length(uFE), Kf=Kf+length(uFE{i}); end;
  % Create dummies and list of names for dummies
  % fl is names of dummies. 1st one is 'cons', next is 'F#_#'
  %e.g, 'F1_2' is dummies for 2nd (unique) value in 1st cat. var.
  xF=ones(N,Kf); fl=cell(1,Kf); %fl{1}='cons'; 
  v=1;  
  for i=1:f
      t=['F' int2str(i) '_'];
    for j=1:length(uFE{i}), xF(:,v)=FE(:,i)==uFE{i}(j); fl{v}=[t int2str(j)]; v=v+1; end;
  end; clear v; 
  
% Index each dummies, ie. col1=dummy 1, col2=dummy2,...
 fv=(1:Kf);
% Remove collinear cols (std=0 [except for constant], this also removes all zeros)
s=std(xF,[],1)<2*N*eps; %get whether indices of each var/col. that have zero std.
t=s([fv]);
fv(s(fv))=[]; % Delete those dummies that have zero std. (We're working on indices.)
Kf=length(fv); % new size of fixed effects
out=xF(:,fv);
% In removing collinear cols, I have deleted constant variable.
% Testing multi colinear problems
% check if rank(xF) = # of vars./cols.
%r = rank(xF); % with tc FE, I have 4412 dummies, but rank = 4403.
% Return the matrix of vars. that are not multi collinear
%out2=base(out1);
end